<?php

namespace App\Http\Controllers\Admin;

use App\Services\Admin\AdminUser;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Auth\ThrottlesLogins;


class LoginController extends Controller
{
    use ThrottlesLogins;

    //登录页面
    public function index()
    {
        return view('admin.login.login');
    }

    //修改页面
    public function member_edit()
    {
        $where[] = ['id', '=', Session::get('session_id')];

        $result = [
            'account' => AdminUser::info($where),
        ];

        return view('admin.member.member', $result);
    }

    //修改登录密码
    public function login_edit_pwd(Request $request)
    {
        $input = $request->input();
        if (empty($input['pwd'])) return self::fail('密码为空');

        $where[] = ['id', '=', Session::get('session_id')];
        $input['pwd'] = Hash::make($input['pwd']);
        AdminUser::update($where, $input);

        return self::success();
    }

    //登录认证
    public function login_do(Request $request)
    {
        $input = $request->input();

        $where[] = ['account',$input['username']];
        $result = AdminUser::info($where);

        if (!isset($result->id))return self::fail('密码错误或者账号错误!');
        if (!Hash::check($input['password'], $result->pwd))return self::fail('密码错误或者账号错误!');

        // 用户信息存入 Session
        \Session::put('session_id', $result->id);
        \Session::put('session_role_name', '超级管理员');

        $admin_role_name = DB::table('admin_role')->where('role_id','=',$result->role_id)->first();
        \Session::put('session_role_name', $admin_role_name->role_name);

        //查询权限
        if($admin_role_name->act_list == 'all'){
            \Session::put('act_list', json_encode(['all']));
            \Session::put('right_list', json_encode(['all']));
            \Session::put('get_routes', json_encode(['all']));
        }else{

            $act_list = explode(',',$admin_role_name->act_list);

            $system_menu = DB::table('system_menu')->whereIn('id', $act_list);

            $right = $system_menu->pluck('right')->toArray();

            $group = $system_menu->groupBy('group')->pluck('group')->toArray();

            //下面的权限是全局的
            $right_list = [
                'LoginController@member_edit',
                'LoginController@login_edit_pwd',
                'LoginController@login_up'
            ];

            foreach ($right as $v){
                $explode_v = explode(',', $v);

                foreach ($explode_v as $vv){
                    $right_list[] = $vv;
                }
            }

            \Session::put('act_list', json_encode($group));                     //大类权限
            \Session::put('right_list', json_encode($right_list));              //该账号有什么权限

            $get_routes = [];

            foreach (app()->routes->getRoutes() as $v){
                if(!isset($v->action['controller']))continue;
                $controller = explode("\\",$v->action['controller']);
                if($controller[3] != 'Admin')continue;

                $controller_count = count($controller);

                if($v->uri == '/')$v->uri = '';

                $uri_string = url_param($v->uri);

                $get_routes[$uri_string] = $controller[($controller_count-1)];
            }

            \Session::put('get_routes', json_encode($get_routes));               //路由对应什么权限
        }

        return self::success();
    }

    //退出登录
    public function login_up()
    {
        \Session::forget('session_id');

        return redirect('/login');
    }

    //定时计算session是否过期
    public function session_time(){
        if(empty(Session::get('session_id')))return self::fail();

        $where[] = ['id',Session::get('session_id')];
        $admin = AdminUser::info($where);
        if(empty($admin))return self::fail();

        return self::success([]);
    }
}
